<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:pkg="http://expath.org/ns/pkg"
                xmlns:web="http://expath.org/ns/webapp"
                xmlns:app="http://fgeorges.org/ns/webapp/tools"
                xmlns:ser="http://fgeorges.org/xslt/serial"
                xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:TODO="to-integrate-in-webapps"
                exclude-result-prefixes="#all"
                version="2.0">

   <xsl:import href="http://fgeorges.org/ns/xslt/serial.xsl"/>
   <xsl:import href="http://fgeorges.org/ns/xslt/serial-html.xsl"/>

   <pkg:import-uri>http://fgeorges.org/ns/webapp/tools/servlets.xsl</pkg:import-uri>

   <!--
       TODO: Add those functions to the EXPath Webapps module spec...
   -->
   <xsl:function name="TODO:is-body-html" as="xs:boolean">
      <xsl:param name="b" as="item()"/>
      <xsl:sequence select="$b instance of document-node(element(html:html))"/>
   </xsl:function>

   <xsl:function name="TODO:is-body-xml" as="xs:boolean">
      <xsl:param name="b" as="item()"/>
      <xsl:sequence select="
          $b instance of document-node()
            and not(TODO:is-body-html($b))"/>
   </xsl:function>

   <xsl:function name="TODO:is-body-text" as="xs:boolean">
      <xsl:param name="b" as="item()"/>
      <xsl:sequence select="$b instance of xs:string"/>
   </xsl:function>

   <xsl:function name="TODO:is-body-binary" as="xs:boolean">
      <xsl:param name="b" as="item()"/>
      <xsl:sequence select="$b instance of xs:base64Binary"/>
   </xsl:function>

   <xsl:function name="TODO:body-type" as="xs:string">
      <xsl:param name="b" as="item()"/>
      <xsl:sequence select="
          if ( TODO:is-body-html($b) ) then
            'html'
          else if ( TODO:is-body-xml($b) ) then
            'xml'
          else if ( TODO:is-body-text($b) ) then
            'text'
          else if ( TODO:is-body-binary($b) ) then
            'binary'
          else
            '#unknown'"/>
   </xsl:function>

   <xsl:function name="app:css-file" as="element(web:response)">
      <xsl:param name="request" as="element(web:request)"/>
      <web:response status="200" message="Ok">
         <web:body content-type="text/css" src="{ ser:resolve-css-uri('nxml') }"/>
      </web:response>
   </xsl:function>

   <xsl:function name="app:dump-request" as="element()+">
      <xsl:param name="input" as="item()+"/>
      <xsl:variable name="request" select="$input[1]"         as="element(web:request)"/>
      <xsl:variable name="bodies"  select="remove($input, 1)" as="item()*"/>
      <web:response status="200" message="Ok">
         <web:body content-type="text/html"/>
      </web:response>
      <html>
         <head>
            <title>Request dump</title>
            <link href="css/serial.css" rel="stylesheet" type="text/css"/>
         </head>
         <body>
            <xsl:sequence select="app:do-dump($request, $bodies)"/>
         </body>
      </html>
   </xsl:function>

   <xsl:function name="app:do-dump" as="element()+">
      <xsl:param name="request" as="element(web:request)"/>
      <xsl:param name="bodies"  as="item()*"/>
      <h2><span>Request dump</span></h2>
      <p>
         <xsl:text>Number of bodies: </xsl:text>
         <xsl:value-of select="count($bodies)"/>
         <xsl:text>.</xsl:text>
      </p>
      <h3>Request object</h3>
      <pre>
         <xsl:sequence select="ser:serialize-to-html($request)"/>
      </pre>
      <xsl:for-each select="$bodies">
         <h3>
            <xsl:text>Body #</xsl:text>
            <xsl:value-of select="position()"/>
         </h3>
         <p>
            <xsl:text>Body type: </xsl:text>
            <xsl:value-of select="TODO:body-type(.)"/>
            <xsl:text>.</xsl:text>
         </p>
         <pre>
            <xsl:sequence select="ser:serialize-to-html(.)"/>
         </pre>
      </xsl:for-each>
   </xsl:function>

   <xsl:function name="app:dump-form" as="element()+">
      <xsl:param name="input" as="element(web:request)"/>
      <web:response status="200" message="Ok">
         <web:body content-type="text/html"/>
      </web:response>
      <html>
         <head>
            <title>Dump form</title>
         </head>
         <body>
            <xsl:sequence select="app:do-form()"/>
         </body>
      </html>
   </xsl:function>

   <xsl:function name="app:do-form" as="element()+">
      <h2>Dump form</h2>
      <h3>method get</h3>
      <p>
         <form action="dump" method="get">
            text: <input name="text" type="text"/><br/>
            file: <input name="file" type="file"/><br/>
            password: <input name="password" type="password"/><br/>
            radio: <input name="radio" type="radio" value="uno">uno</input>
            <input name="radio" type="radio" value="dos">dos</input><br/>
            checkbox: <input name="checkbox" type="checkbox" value="aaa">aaa</input>
            <input name="checkbox" type="checkbox" value="bbb">bbb</input><br/>
            <input type="submit" value="Submit"/>
         </form>
      </p>
      <h3>url encoded</h3>
      <p>
         <!-- enctype="application/x-www-form-urlencoded" is the default -->
         <form action="dump" method="post">
            text: <input name="text" type="text"/><br/>
            file: <input name="file" type="file"/><br/>
            password: <input name="password" type="password"/><br/>
            radio: <input name="radio" type="radio" value="uno">uno</input>
            <input name="radio" type="radio" value="dos">dos</input><br/>
            checkbox: <input name="checkbox" type="checkbox" value="aaa">aaa</input>
            <input name="checkbox" type="checkbox" value="bbb">bbb</input><br/>
            <input type="submit" value="Submit"/>
         </form>
      </p>
      <h3>form data</h3>
      <p>
         <form action="dump" method="post" enctype="multipart/form-data">
            text: <input name="text" type="text"/><br/>
            file: <input name="file" type="file"/><br/>
            password: <input name="password" type="password"/><br/>
            radio: <input name="radio" type="radio" value="uno">uno</input>
            <input name="radio" type="radio" value="dos">dos</input><br/>
            checkbox: <input name="checkbox" type="checkbox" value="aaa">aaa</input>
            <input name="checkbox" type="checkbox" value="bbb">bbb</input><br/>
            <input type="submit" value="Submit"/>
         </form>
      </p>
   </xsl:function>

</xsl:stylesheet>
